//
//  KKNetwork.m
//  Questionnaire
//
//  Created by leo on 16/1/24.
//  Copyright © 2016年 leo. All rights reserved.
//

#import "KKNetworkManager.h"

@implementation KKNetworkManager

#pragma mark - Private methods

+ (NSMutableURLRequest*)requestWithURL:(NSString *)urlString {
    
    if (urlString == nil) {
        return nil;
    }
    NSURL *url = [NSURL URLWithString: urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setTimeoutInterval:30];
    
    return request;
}

#pragma mark - Post methods

+ (void)sendPostRequest:(KKNetworkRequest *)request success:(KKNetworkSuccessHandle)successful faile:(KKNetworkFaileHandle)faile {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: true];
    
    NSMutableURLRequest *URLRequest = [self requestWithURL: request.requestURL];
    [URLRequest setHTTPBody: [request postData]];
    
    NSLog(@".....%@", [[NSString alloc] initWithData: [request postData] encoding: NSUTF8StringEncoding]);
    
    NSURLSession *session = [NSURLSession sharedSession];

    //  子线程运行
    NSURLSessionDataTask *task = [session dataTaskWithRequest: URLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        //  回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: false];
            
            if (faile && error) {
                faile(error.description);
                return;
            }
            
            NSString *parserError = nil;
            KKNetworkResponse *responseObject = [KKNetworkResponse responseWithData: data request:request error: &parserError];
            
            if (responseObject == nil && parserError != nil) {
                if (faile) {
                    faile(parserError);
                }
            }
            else {
                if (successful) {
                    successful(responseObject);
                }
            }
        });
    }];
    
    [task resume];
}


+ (void)sendPostRequestWithAFNetworking:(KKNetworkRequest *)request success:(KKNetworkSuccessHandle)successful faile:(KKNetworkFaileHandle)faile {
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 15.0;
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    
    [manager POST: request.requestURL parameters: request.requestParmeters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSLog(@"responseObject = %@", responseObject);
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
        NSLog(@"error = %@", error);
    }];
    
}

@end
